#pragma once 
#include <sys/epoll.h>
#include "Log.hpp"
#include "Commn.hpp"

namespace EpollerModule
{
    using namespace LogModule;
    const int gfd = -1;
    class EpollerServer
    {
        private:
            int _epollfd;
        public:
            EpollerServer() :_epollfd(gfd)
                {
                    _epollfd = ::epoll_create(256);
                    if (_epollfd < 0){
                        LOG(LogLevel::ERROR) << "epoll_create error";
                        DIE(EPOLL_CREATE_ERROR);
                    }
                    LOG(LogLevel::INFO) << "epoll create success: " << _epollfd;
                }
            void CTL(int fd,uint32_t e,int flg)
            {
                //这里直接使用局部的ctl也可以!
                struct epoll_event event;
                event.events = e;
                event.data.fd = fd;
                int n = ::epoll_ctl(_epollfd,flg,fd,&event);
                if (n < 0)
                {
                    LOG(LogLevel::ERROR) << "epoll_ctrl fd: " << fd << "error";
                }
            }
            int  Wait(struct epoll_event* events,int maxvel)
            {
                //这里我们直接返回就可以了，后面分派的时候循环从0开始遍历
                int n = ::epoll_wait(_epollfd,events,maxvel,0);
                if (n < 0)
                {
                    LOG(LogLevel::ERROR) << "epoll_wait error";
                }
                return n;
            }
            void Add(int fd,uint32_t e)
            {
                CTL(fd,e,EPOLL_CTL_ADD);
            }
            void Modify(int fd,uint32_t e)
            {
                CTL(fd,e,EPOLL_CTL_MOD);
            }
            void Delete(int fd)
            {
                CTL(fd,0,EPOLL_CTL_DEL);
            }
            ~EpollerServer(){}
    };
};



